package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.aixingfu.sports.MVP.contract.RenewContractContract;
import com.aixingfu.sports.MVP.presenter.RenewContractPresenter;
import com.aixingfu.sports.Model.RenewContractModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.view.NoScrollWebView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orie.librarybase.base.BaseActivity;
import com.orie.librarybase.net.HttpConfig;
import com.orie.librarybase.utils.UserUtils;
import com.vondear.rxtool.view.RxToast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//续费合同
@Route(path = "/main/ConfirmContractRenewActivity")
public class ConfirmContractRenewActivity extends BaseActivity implements RenewContractContract.MyView {
    @BindView(R.id.tv_title)
    TextView tvTitle;
        @BindView(R.id.webview)
        NoScrollWebView webview;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.tv_next)
    TextView tvNext;
//    @BindView(R.id.lin_web)
//    LinearLayout linWeb;
    private RenewContractPresenter renewContractPresenter;
    private String order_id = "";
    private String contract_id = "";
    private String renewal_way = "";
    private boolean misChecked;
//    private AgentWeb agentWeb;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_confirm_contract_renew;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/ConfirmContractRenewActivity").navigation();
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        renewContractPresenter = new RenewContractPresenter();
        renewContractPresenter.attachView(this);
        Bundle bundle = getIntent().getExtras();
        order_id = bundle.getString("order_id", "");
        contract_id = bundle.getString("contract_id", "");
//        getOrderDeal();
        WebSettings settings = webview.getSettings();
        webview.setVerticalScrollBarEnabled(false);
        //设置webView里字体大小
        settings.setSupportZoom(true);
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
        }
        settings.setBlockNetworkImage(false);
        if(Build.VERSION.SDK_INT >= 19) {
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if(!settings.getLoadsImagesAutomatically()) {
                    settings.setLoadsImagesAutomatically(true);
                }
            }
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();//接受所有网站的证书
            }
        });
        webview.loadUrl(HttpConfig.BASEURL + "/v2/renew-contract/get-renew-contract" + "?accesstoken=" + UserUtils.getToken(ConfirmContractRenewActivity.this) + "&order_id=" + order_id);//加载url
        checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                misChecked = isChecked;
            }
        });

//        agentWeb=AgentWeb.with(this)
//                .setAgentWebParent(linWeb, new LinearLayout.LayoutParams(-1, -1))
//                .useDefaultIndicator()//进度条
//                .createAgentWeb()
//                .ready()
//                .go(HttpConfig.BASEURL + "/v2/renew-contract/get-renew-contract" + "?accesstoken=" + UserUtils.getToken(ConfirmContractRenewActivity.this) + "&order_id=" + order_id);

    }

    public void getOrderDeal() {
        if (renewContractPresenter.isViewAttached()) {
            HashMap<String, String> map = new HashMap<>();
            map.put("order_id", order_id);
            renewContractPresenter.getRenewContract(map);
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void beforeInit() {

    }

    @Override
    public void showToast(String msg, String state) {

    }

    @Override
    public void setRenewContract(RenewContractModel data) {

    }

    @Override
    public void showLoading() {
showLoadingDialog();
    }

    @Override
    public void hideLoading() {
dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_next)
    public void onViewClicked() {
        if (misChecked == true) {
            ARouter.getInstance().build("/main/RenewSignActivity").withString("order_id", order_id).withString("contract_id", contract_id).navigation();
        } else {
            RxToast.normal(ConfirmContractRenewActivity.this, "请勾选同意按钮").show();
        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
